package com.example.controller;

import com.example.service.MyService;
import com.example.vo.ProcessReqVO;
import com.example.vo.TaskInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/process")
@RestController
public class MyRestController {

    private String modelKeyOfDemo = "demo";
    @Autowired
    private MyService myService;

    @PostMapping(value = "/process")
    public void startProcessInstance(@RequestBody StartProcessRepresentation startProcessRepresentation) throws JsonProcessingException {
        myService.startProcess(startProcessRepresentation.getAssignee());
    }

    @PostMapping(value = "/start")
    public String startProcess(@RequestBody StartProcessRepresentation startProcessRepresentation) throws JsonProcessingException {
        return myService.startProcess(startProcessRepresentation.getAssignee(), startProcessRepresentation.getModelKey());
    }

    @RequestMapping(value = "/tasks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TaskRepresentation> getTasks(@RequestParam String assignee) {
        List<Task> tasks = myService.getTasks(assignee);
        List<TaskRepresentation> dtos = new ArrayList<TaskRepresentation>();
        for (Task task : tasks) {
            dtos.add(new TaskRepresentation(task.getId(), task.getName()));
        }
        return dtos;
    }

    @RequestMapping(value = "/complete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void completeTask(@RequestParam String taskId) {
        myService.completeTask(taskId);
        // todo 根据taskID找到流程id，然后根据流程id从act_ru_task表查询待处理的任务id列表（并行任务时会生成多个任务id）

    }

    @RequestMapping(value = "/completeTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void completeTask(@RequestParam String taskId, @RequestBody Map<String, Object> params) throws Exception {
        myService.completeTask(taskId, params);
    }

    @RequestMapping(value = "/rejectTask", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public void rejectTask(@RequestParam String taskId, @RequestBody Map<String, Object> params) throws Exception {
        myService.rejectTask(taskId, params);
    }

    @RequestMapping(value = "/currentHandlerWithTaskId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public void currentHandler(@RequestParam String taskId, @RequestHeader("token") String token) {
        myService.currentHandler(taskId);
    }

    @RequestMapping(value = "/currentHandler", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TaskInfo> currentHandler(@RequestBody ProcessReqVO reqVO, @RequestHeader("token") String token) {
        return myService.currentHandler(reqVO);
    }

    @RequestMapping(value = "/todoList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TaskInfo> myTodoList(@RequestBody ProcessReqVO reqVO, @RequestHeader("token") String token) {
        return myService.myTodoList(reqVO);
    }

    static class TaskRepresentation {

        private String id;
        private String name;

        public TaskRepresentation(String id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }

    static class StartProcessRepresentation {

        private String assignee;

        public String getModelKey() {
            return modelKey;
        }

        public void setModelKey(String modelKey) {
            this.modelKey = modelKey;
        }

        private String modelKey;

        public String getAssignee() {
            return assignee;
        }

        public void setAssignee(String assignee) {
            this.assignee = assignee;
        }
    }
}